#include "Stdafx.h"
#include "SWElement.h"
#include "SWMemoryDC.h"
#include "SWViewConst.h"
#include "SWViewWnd.h"

BOOL g_bTracking = FALSE;

CSWElement::CSWElement()
{
	m_pWnd   = NULL;
	m_bShow  = FALSE;
	m_status = 0;
	m_iID    = 0;
	
	for( int i = 0 ; i < ES_END ;  i ++ )  
		m_bmp_arry[i] = NULL;

}

CSWElement::~CSWElement()
{
	for( int i = 0 ; i < ES_END ;  i ++ ) { 
		delete m_bmp_arry[i] ; 
	}
}


BOOL CSWElement::Create(const CRect &rc,CWnd *pWnd,const INT iID)
{
	m_pWnd = pWnd;
	m_Rc   = rc;
	m_iID  = iID;
	return TRUE;
}

void CSWElement::SetEventMask(UINT nEvent,const int iResourceID)
{
	IStream *pStream = UiCreateHBitmapFromResourceID(
		AfxGetInstanceHandle(),iResourceID,_T("PNG") );

	Gdiplus::Bitmap *bmp = m_bmp_arry[nEvent] ;
	if( bmp) {
		SAFE_DELETE(bmp);
	}
	bmp = new Gdiplus::Bitmap(pStream);
	m_bmp_arry[nEvent] = bmp;
}

void CSWElement::DoDraw(CDC *pDC)
{
	if( !m_bShow )
		return ;

	Gdiplus::Bitmap *pBmp = m_bmp_arry[m_status];
	if( !pBmp)
		return ;

	Gdiplus::Graphics g(pDC->GetSafeHdc());
	g.DrawImage( pBmp,m_Rc.left,m_Rc.top,m_Rc.Width(),m_Rc.Height());
}

BOOL CSWElement::WindowProc(UINT message,WPARAM wParam ,LPARAM lParam,HRESULT &hr)
{
	if( !m_bShow )
		return FALSE;

	switch( message)
	{
	case WM_MOUSEMOVE:
		{
			CPoint pt( GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
			int iOld = m_status;
			if( m_Rc.PtInRect(pt))
			{
				m_status = ES_OVER;
				SetCursor(::LoadCursor(NULL,IDC_HAND));
			}
			else
			{
				SetCursor(::LoadCursor(NULL,IDC_ARROW));
				m_status = ES_NORMAL;
			}
			if( iOld != m_status ){
				Update();
			}

			if (!g_bTracking) 
			{
				g_bTracking = UiTrackMouseEvent(m_pWnd->GetSafeHwnd(),TME_LEAVE|TME_HOVER,1);
			}
		}
		break;

	case WM_MOUSELEAVE:
		{
			g_bTracking = FALSE;
			//SetCursor( ::LoadCursor(AfxGetInstanceHandle(),IDC_ARROW ) );
			m_status = ES_NORMAL;
			Update(FALSE);
		}
		break;

	case WM_LBUTTONDOWN:
		{
			CPoint pt( GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
			int iOld = m_status;
			if( m_Rc.PtInRect(pt)) 
			{
				m_status = ES_DOWN;
				hr = HTCLIENT;
				m_pWnd->SetCapture();
				return TRUE;
			}
			if( iOld != m_status ){
				Update();
			}
		}
		break;
	case WM_LBUTTONUP:
		{
			if( m_pWnd->GetCapture() )
				::ReleaseCapture();

			CPoint pt( GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
			int iOld = m_status;
			if( m_Rc.PtInRect(pt)) 
			{
				ShellExecute(NULL, _T("open"),_T("http://www.pngview.com"), NULL, NULL, SW_SHOW);
				m_status = ES_OVER;
			}
			else{
				m_status = ES_NORMAL;
			}
			if( iOld != m_status ){
				Update();
			}
		}
		break;

	case WM_SETCURSOR:
		{
			POINT pt;
			::GetCursorPos(&pt);
			::ScreenToClient(m_pWnd->GetSafeHwnd(),&pt);
			if( m_Rc.PtInRect(pt)  ) 
			{
				
				SetCursor(::LoadCursor(NULL,IDC_HAND ));
				return TRUE;
			}
		}
		break;

	default:
		break;
	}

	return FALSE;
}

void CSWElement::Update(BOOL bShow )
{
	CSWViewMainWnd* pWnd = (CSWViewMainWnd*)m_pWnd;
	pWnd->Update();
	pWnd->UpdateShowWindow(bShow);
}